<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>营销</el-breadcrumb-item>
      <el-breadcrumb-item>广告列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 30px;">
      <div>
        <el-form :inline="true" class="demo-form-inline">
          <i class="el-icon-search"></i>
          <span>筛选搜索</span>
          <el-form-item label="商品Id" style="margin-top: 30px;">
            <el-input placeholder="" v-model="input" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <!-- <el-button type="primary">查询</el-button> -->
            <el-button
              type="primary"
              @click="handleAdd"
              style="margin-top: 30px;"
            >
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <el-table :data="tables" style="width: 100%;" border>
      <el-table-column label="Id" width="180" prop="id" type="index">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px;">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品Id" width="180" prop="goodsId" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.goodsId }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="图片" width="250" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <img
                :src="'http://localhost:5000/' + scope.row.inTurnPictures"
                alt=""
                width="200px"
                height="100px"
              />
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <!-- scope.row  代表一行的数据 scope.row.msg_state 代表状态的值 -->
          <!-- {{scope.row}} -->
          <el-switch
            v-model="scope.row.isActived"
            @change="onChange(scope.$index, scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <el-dialog title="轮播图" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="商品Id" :label-width="formLabelWidth">
          <el-input v-model="formData.goodsId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            ref="upload"
            :auto-upload="false"
            class="upload-demo"
            :limit="1"
            action="#"
            :on-remove="imgBroadcastRemove"
            list-type="picture"
            :on-change="imgBroadcastChange"
            :http-request="handleUploadAvatar"
            accept="image/jpg,image/png,image/jpeg"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="handleSave"
          style="margin-left: 250px;"
        >
          确 定
        </el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <div class="block">
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getList,
  addInturn,
  modInturn,
  deleteInturn,
  vimState,
} from '../../api/advertisement'

export default {
  name: 'imgUpload',

  data() {
    return {
      input: '',
      formData: {
        goodsId: '',
        inTurnPictures: '',
        isActived: true,
      },
      pager: {
        pageIndex: 1,
        pageSize: 5,
        rowsTotal: 30,
      },
      formLabelWidth: '120px',
      currentIndex: 0,
      dialogFormVisible: false,
      tableData: [],
    }
  },

  computed: {
    tables() {
      const input = this.input
      if (input) {
        console.log('input输入的搜索内容：' + this.input)
        return this.tableData.filter((data) =>
          JSON.stringify(data.goodsId).includes(this.input),
        )
      }

      return this.tableData
    },
  },
  mounted() {
    this.fetchData(this.pager)
    setTimeout(() => {
      console.log(this.tableData)
    }, 1000)
  },

  methods: {
    //状态
    onChange(index, row) {
      console.log(index, row)
      this.formData.isActived = row.isActived
      vimState(row.id, this.formData).then((data) => {
        console.log(data)
        if (data.data.isActived == false) {
          console.log(data.msg)
          this.$message.error(data.msg)
        } else {
          console.log(data.msg)
          this.$message.success(data.msg)
        }
      })
    },

    // 删除用户
    handleDelete(index, row) {
      //删除指定id用户
      this.tableData.splice(index, 1)
      //相应删掉id的数据
      deleteInturn(row.id)
      //相应的表数据要减掉一条数据
      this.pager.rowsTotal = this.pager.rowsTotal - 1
      this.$message('删除成功')
      console.log(index, row)
      console.log(index, row)
    },
    //添加
    handleAdd() {
      this.dialogFormVisible = true
      this.formData.id = 0
      this.formData.goodsId = ''
      this.formData.inTurnPictures = ''
    },

    handleEdit(index, row) {
      this.dialogFormVisible = true
      this.formData.id = row.id
      this.formData.goodsId = row.goodsId
      this.formData.inTurnPictures = row.inTurnPictures

      // 编辑时获得当前表数组中的下标
      this.currentIndex = index
      console.log(index, row)
    },

    // 有图片移除后 触发
    imgBroadcastRemove(file, fileList) {
      this.formData.imgBroadcastList = fileList
    },
    // 提交弹窗数据
    handleSave() {
      this.$refs.upload.submit()
    },
    handleUploadAvatar(file) {
      console.log(file)
      if (this.formData.id == 0) {
        // 添加
        const fileObj = file.file
        const form1 = new FormData()
        form1.append('goodsId', this.formData.goodsId)
        form1.append('imageFile', fileObj)

        addInturn(form1).then(({ data }) => {
          console.log(data)
          console.log(this.tableData)
          this.tableData.push(data)
          this.$message.success('添加成功')
        })
        this.dialogFormVisible = false
        this.$refs.upload.clearFiles()
      } else {
        //编辑
        console.log(file.file)
        console.log(this.formData.id)
        const fileObj = file.file
        const form1 = new FormData()
        form1.append('imageFile', fileObj)
        form1.append('goodsId', this.formData.goodsId)
        modInturn(this.formData.id, form1).then(({ data }) => {
          this.tableData.splice(this.currentIndex, 1, data)
          this.dialogFormVisible = false
          this.$message.success('修改成功')
        })
        this.$refs.upload.clearFiles()
      }
    },
    //取消
    handleCancel() {
      this.dialogFormVisible = false
    },
    imgBroadcastChange(file, fileList) {
      console.log(file)
      console.log(fileList)
    },
    // 页面页码改变时，重新拉取数据
    handlePageIndexChange(val) {
      console.log(val)
      this.pager.pageIndex = val > 0 ? val : 1
      this.fetchData(this.pager)
    },
    // 页面大小改变时，重新拉取数据
    handlePageSizeChange(val) {
      console.log(val)
      this.pager.pageSize = val
      this.fetchData(this.pager)
    },

    // 拉取后台数据（带分页）
    fetchData(pager) {
      getList(pager).then((data) => {
        console.log(data)

        this.tableData = data.data.data
        console.log(this.tableData)
        this.pager = data.data.pager
      })
    },
  },
}
</script>

<style></style>
